/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 */
FNISVGEditorLoadDocumentPlugin = Class.create(FNISVGEditorPlugin, {
  initialize : function($super, editorInstance, pluginURL) {
    $super(editorInstance, pluginURL);
    var button = new FNIPushButton( {});
    button.domNode.addClassName('fniSVGEditor_LoadDocumentPlugin_Button');
    button.addListener('buttonClick', this, this._onButtonClick);
    button.setTooltip(SVGLocale.getText("loaddocumentPlugin_buttonTooltip"));
    button.setImage(this.getPluginURL() + "/gfx/icons/22x22/actions/x-office-drawing-open.png");
    editorInstance.registerToolbarItem('loadDocument', button);
  },
  getName : function() {
    return "loaddocument";
  },
  _onButtonClick : function(event) {
    var dialog = this.getEditorInstance().openDialog({
      contentURL: this.getPluginURL() + '/dialog.html',
      iconURL: this.getPluginURL() + "/gfx/icons/16x16/actions/x-office-drawing-open.png",
      title: SVGLocale.getText("loaddocumentPlugin_dialogTitle"),
      customCSS: this.getPluginURL() + '/dialog.css'
    });
    
    var _this = this;
    
    dialog.addListener("onLoad", function (event) {
      var okButton = dialog.getFrameDocument().getElementById('loadDocumentOkButton');
      var cancelButton = dialog.getFrameDocument().getElementById('loadDocumentCancelButton')
      Event.observe(okButton, "click", function (event) {
        var svgURL = dialog.getFrameDocument().getElementById('loadDocumentURLInput').value;
        var append = dialog.getFrameDocument().getElementById('loadDocumentAppendInput').checked;
        
        new Ajax.Request(svgURL, {
          onSuccess: function(transport) {                                       
            var contentType = transport.getResponseHeader('Content-type');
            if ((transport.responseXML) && (contentType == 'image/svg+xml')) {
              if (append != true) {
                _this.getEditorInstance().getSVGDocument().removeAllElements();
                _this.getEditorInstance().getUndoManager().clearStack();
              }
              
              _this.getEditorInstance().appendExternalSVGDocument(transport.responseXML);
              
              dialog.close();        
            } else {
              alert(SVGLocale.getText("loaddocumentPlugin_sourceNotSVGImage", contentType));
            }
          },
          onFailure: function (transport) {
            alert(SVGLocale.getText("loaddocumentPlugin_couldNotFindImage"));
          }
        }); 
      });
             
      Event.observe(cancelButton, "click", function (event) {
        dialog.close();
      });
      
      dialog.localize();
      dialog.setHeight(128);
      dialog.center();
      dialog.show();
    });
  }
});

document.fire("svgEditorPluginLoaded:loaddocument", {
  pluginClass : FNISVGEditorLoadDocumentPlugin
});
